package com.bbs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bbs.mapper.EventMapper;
import com.bbs.model.Event;

/**
 * @author piggsoft
 * @e-mail piggsoft@163.com
 * @since 2013-4-29
 * @version 1.0
 */
@Service
public class EventService {

	private EventMapper eventMapper;

	@Transactional
	public Integer addEvent(Event event) {
		String url = event.getUrl();
		if	(StringUtils.isNotEmpty(url)){
			if(!url.startsWith("http://")) {
				url = "http://" + url;
				event.setUrl(url);
			}
		}
		eventMapper.insert(event);
		return event.getId();
	}

	public List<Event> searchEvent(final long startTime, final long endTime) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("startTime", startTime);
		parameters.put("endTime", endTime);
		return eventMapper.search(parameters);
	}
	
	@Transactional
	public void updateEvent(Event event) {
		eventMapper.updateByPrimaryKeySelective(event);
	}
	
	@Transactional
	public void delEvent(Event event) {
		eventMapper.deleteByPrimaryKey(event.getId());
	}
	
	@Resource
	public void setEventMapper(EventMapper eventMapper) {
		this.eventMapper = eventMapper;
	}
	
}
